// $codepro.audit.disable
//Not part of source code 
import static org.junit.Assert.*;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Window;

import javax.swing.JDialog;
import javax.swing.JFrame;

import junit.framework.Assert;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class TestSpaceTrader {

    /**
     * This test case was written by Julia Deeb
     */
    @Test
    public void testCanTravel() {
	Game g = new Game();
	Game.Player = new Player();
	Game.Player.createPlayer("Julia", new int[4]);
	Game.Player.setShip(new Ship("Flea"));
	Space s = new Space(2, new Dimension(10, 10), Game.Player);
	Planet[] loc = s.getPlanets();
	assertTrue(s.canTravel(Game.Player.getCurrentPlanet()));
	assertTrue(s.canTravel(loc[0]));
	assertTrue(s.canTravel(loc[1]));

	Game.Player.getShip().useFuel(Game.Player.getShip().getCurrFuelLevel());
	int notMyLoc = 0;
	int count = 0;
	for (Planet p : loc) {
	    if (!p.equals(Game.Player.getCurrentPlanet())) {
		notMyLoc = count;
	    }
	    count++;
	}
	assertFalse(s.canTravel(loc[notMyLoc]));

	try {
	    s.canTravel(null);
	    assertTrue(false);
	} catch (NullPointerException e) {
	    assertTrue(true);
	}
    }

    /**
     * This test case was written by Taylor Garren
     */
    @Test
    public void tgarrenTest() {
	// Initial set-up
	Game game = new Game();
	int[] initSkillTable = { 4, 4, 4, 4 };
	Game.Player = new Player();
	Game.Player.createPlayer("User", initSkillTable);
	Ship ship = new Ship("Flea");
	Game.Player.setPlayerShip(ship);
	Resource[] cargo = ship.getCargo();
	Planet planet = new Planet("TestPlanet", new Point(0, 0), Game.Player);
	Marketplace marketplace = new Marketplace(planet, Game.Player);
	Game.Player.setWallet(10000000);
	System.out.println("Your wallet is: " + Game.Player.getWallet());

	// Tests begin

	// 1) Testing that it is possible to buy all types of items at a given
	// planet
	double[] priceArray = marketplace.getPriceArray();
	Resource[] resources = marketplace.getResources();
	for (int i = 0; i < priceArray.length; i++) {
	    System.out.println("Price of the item is: " + priceArray[i]);
	    System.out.println(priceArray[i] * 5);
	    assertEquals(true,
		    marketplace.canBuy(i, resources[i].getMultiplicity() % 5));
	}

	// 2) Testing invalid inputs
	try {
	    marketplace.canBuy(10, 1000);
	    assertTrue(false);
	} catch (IndexOutOfBoundsException e) {
	    assertTrue(true);
	}
	assertEquals(false, marketplace.canBuy(0, -1000));
	Game.Player.setWallet(-1.0);
	for (int i = 0; i < priceArray.length; i++) {
	    assertEquals(false, marketplace.canBuy(i, 1));
	}

	// 3) Testing that you cannot buy any item with insufficient funds.
	Game.Player.setWallet(0.0);
	for (int i = 0; i < priceArray.length; i++) {
	    assertEquals(false, marketplace.canBuy(i, 1));
	}
	Game.Player.setWallet(-1.0);
	for (int i = 0; i < priceArray.length; i++) {
	    assertEquals(false, marketplace.canBuy(i, 1));
	}
	printArray(priceArray);
	
	// 4) Testing that you cannot add more cargo than capacity
	Game.Player.setWallet(100000000);
	ship.setTotalCargo(10);
	for (int i = 0; i < priceArray.length; i++) {
	    assertEquals(false, marketplace.canBuy(i, 1));
	}
	
	/* 5) Testing that you cannot buy more of a resource than exists
	 on the planet */
	for (int i = 0; i < priceArray.length; i++) {
	    assertEquals(false, marketplace.canBuy(i, 101));
	}
    }
    
    /**
     * This test case was written by Andrew Darrohn
     * Testing CalcPrices() in Planet
     */
    
    @Test
    public void adarrohn3Test()
    {
	Game g = new Game();
	Game.Player = new Player();
	Ship ship = new Ship("Flea");
	Game.Player.createPlayer("asdf", new int[4]);
	Game.Player.setShip(ship);
	Space s = new Space(2, new Dimension(10, 10), Game.Player);
	
	//Testing first branch of if
	Assert.assertEquals(0, ship.convertToType("Flea"));
	
	//Testing else branch
	Assert.assertEquals(-1, ship.convertToType("asdfgfsgsdgsdgsdfg"));
    }
    
    
    public void printArray(double[] array) {
	System.out.println("Congrats!");
	String result = "[ ";
	for (int i = 0; i < array.length; i++) {
	    result += array[i] + " ";
	}
	System.out.println(result + "]");
    }
    
    /**
     * This test case was written by Nick (Nicolai) Popescu
     * It tests the fuelShip method from SpacePort.
     * NOTE: The JDialogue boxes are spawned by my tests, not those of others.
     * If anyone is to lose points for this, it should be me.
     */
    
    @Test
    public void fuelShipTest(){
	
	/*
	 * set-up
	 */
        Game game = new Game();
        Game.Player = new Player();
        Game.Player.createPlayer("Nick",new int[]{13,1,1,1});
        Game.Player.setShip(new Ship("Flea"));
        Ship ship = Game.Player.getPlayerShip();
        SpacePort spacePort = new SpacePort();
        Game.InitConfigScreen = new InitConfigScreen();
        
        /*
         * test with sufficient funds
         */
        
        //Ship starts with full fuel
        assertEquals("Ship starts with full fuel", ship.getThisFuelSize(), ship.getCurrFuelLevel());
        
        //Ship with 0 fuel refueled to full capacity
        ship.useFuel(ship.getCurrFuelLevel());
        assertEquals("Ship has 0 fuel", 0, ship.getCurrFuelLevel());
        
        spacePort.fuelShip(ship.getThisFuelSize());
        assertEquals("Ship fully fueled", ship.getThisFuelSize(), ship.getCurrFuelLevel());
        
//        for(Window w:Game.InitConfigScreen.getFrame().getOwnedWindows()){
//            if ( w instanceof JDialog) {
//			System.out.println(((JDialog)w).getTitle());
//            };
//        }
        
      //Ship with full fuel not fueled beyond capacity
        ship.useFuel(ship.getCurrFuelLevel());
        ship.addFuel(ship.getThisFuelSize());
        assertEquals("Ship has full fuel", ship.getThisFuelSize(), ship.getCurrFuelLevel());
        
        spacePort.fuelShip(ship.getThisFuelSize());
        assertEquals("Ship still has max fuel", ship.getThisFuelSize(), ship.getCurrFuelLevel());
      
      //Ship with 1 fuel fueled up to capacity
        ship.useFuel(ship.getCurrFuelLevel());
        ship.addFuel(1);
        assertEquals("Ship has 1 fuel", 1, ship.getCurrFuelLevel());
        
        spacePort.fuelShip(ship.getThisFuelSize());
        assertEquals("Ship has max fuel", ship.getThisFuelSize(), ship.getCurrFuelLevel());
      
      //Ship with only 1 missing fuel fueled up to capacity
        ship.useFuel(ship.getCurrFuelLevel());
        ship.addFuel(ship.getThisFuelSize()-1);
        assertEquals("Ship only missing 1 fuel", ship.getThisFuelSize()-1, ship.getCurrFuelLevel());
        
        spacePort.fuelShip(ship.getThisFuelSize());
        assertEquals("Ship has max fuel", ship.getThisFuelSize(), ship.getCurrFuelLevel());
      
      /*
       * test with insufficient funds
       */
      
      //Try to fill ship without money
        ship.useFuel(ship.getCurrFuelLevel());
        assertEquals("Ship has no fuel", 0, ship.getCurrFuelLevel());
        Game.Player.setWallet(0);
        
        spacePort.fuelShip(ship.getThisFuelSize());
        assertEquals("Ship still has no fuel", 0, ship.getCurrFuelLevel());
        
        assertEquals("Player's wallet is still 0", 0, (int)Game.Player.getWallet());
        
      //Try to fill ship for only half a tank with sufficient money
        ship.useFuel(ship.getCurrFuelLevel());
        assertEquals("Ship has no fuel", 0, ship.getCurrFuelLevel());
        Game.Player.setWallet(60);
        
        spacePort.fuelShip(ship.getThisFuelSize()/2);
        assertEquals("Ship now has half a tank", ship.getThisFuelSize()/2, ship.getCurrFuelLevel());
        
        assertEquals("Player's wallet is now 0", 30, (int)Game.Player.getWallet());
        
      //Try to fill ship for half a tank with just enough money
        ship.useFuel(ship.getCurrFuelLevel());
        assertEquals("Ship has no fuel", 0, ship.getCurrFuelLevel());
        Game.Player.setWallet(31);
        
        spacePort.fuelShip(ship.getThisFuelSize()/2);
        assertEquals("Ship now has half a tank", ship.getThisFuelSize()/2, ship.getCurrFuelLevel());
        
        assertEquals("Player's wallet is now 1", 1, (int)Game.Player.getWallet());
    }
}
